package com.zx.mobile.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zx.map.dao.model.User;
import com.zx.map.dao.model.UserCount;
import com.zx.map.service.UserCountService;
import com.zx.map.service.UserService;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.mobile.domain.MobileUserDomain;
import com.zx.mobile.utils.WebUtils;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private UserCountService userCountService;

	@RequestMapping("/delete") // 会员删除
	@ResponseBody
	public Map userDel(String userid) {
		try {
			userService.delete(userid);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

		return CommonResponseUtils.successResponse();
	}

	@RequestMapping("/add") // 会员增加
	@ResponseBody
	public Map userAdd(@RequestBody User user) {
		try {
			userService.add(user);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
		return CommonResponseUtils.successResponse();
	}

	@RequestMapping("/update") // 会员修改
	@ResponseBody
	public Map userUpdate(@RequestBody User user) {
		try {
			userService.update(user);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
		return CommonResponseUtils.successResponse();
	}

	@RequestMapping("/count") // 会员统计
	@ResponseBody
	public Map userCount(@RequestBody User user, HttpServletRequest request) {
		try {
			MobileUserDomain mUserDomain = WebUtils.queryCurrentLoginInfo(request);
			String shopId = "abc";
			if (mUserDomain != null && mUserDomain.getMerchantInfoDetailModel() != null
					&& mUserDomain.getMerchantInfoDetailModel().getMerchantId() != null) {
				shopId = mUserDomain.getMerchantInfoDetailModel().getMerchantId();
			}
			List<String> ids = new ArrayList<String>();

			Date begin = null;
			Date end = null;
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (user.getBeginDate() != null) {

				begin = formatter.parse(user.getBeginDate());
			}
			if (user.getEndDate() != null) {
				end = formatter.parse(user.getEndDate());
			}

			List<User> ulist = userService.query(shopId, begin, end);
			for (User u : ulist) {
				ids.add(u.getId());
			}
			return CommonResponseUtils.successResponse(ids);
		} catch (ParseException e) {
			return CommonResponseUtils.failureResponse("日期转换错误");
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
	}

	// @RequestMapping("/info") // 会员列表
	// @ResponseBody
	// public Map userInfo(@RequestBody User user, HttpServletRequest request) {
	// try {
	// MobileUserDomain mUserDomain = WebUtils.queryCurrentLoginInfo(request);
	// String shopName = "";
	// if (mUserDomain != null && mUserDomain.getMerchantInfoDetailModel() != null
	// && mUserDomain.getMerchantInfoDetailModel().getMerchantName() != null) {
	// shopName = mUserDomain.getMerchantInfoDetailModel().getMerchantName();
	// }
	// List<User> ulist = userService.query(shopName, user.getPageSize(),
	// user.getPageNum());
	// return CommonResponseUtils.successResponse(ulist);
	// } catch (Throwable e) {
	// e.printStackTrace();
	// return CommonResponseUtils.failureResponse(e.getMessage());
	// }
	//
	// }

	@RequestMapping("/info") // 会员列表
	@ResponseBody
	public Map userInfo(@RequestBody User user, HttpServletRequest request) {

		try {
			MobileUserDomain mUserDomain = WebUtils.queryCurrentLoginInfo(request);
			String shopId = "";
			if (mUserDomain != null && mUserDomain.getMerchantInfoDetailModel() != null
					&& mUserDomain.getMerchantInfoDetailModel().getMerchantId() != null) {
				shopId = mUserDomain.getMerchantInfoDetailModel().getMerchantId();
			}
			user.setMerchantid(shopId);
			List<UserCount> ulist = userCountService.list(user);
			return CommonResponseUtils.successResponse(ulist);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}

	@RequestMapping("/history") // 会员到店统计
	@ResponseBody
	public Map userHistory(@RequestBody User user) {
		userService.update(user);
		return CommonResponseUtils.successResponse();
	}

	@RequestMapping("/historycount")
	@ResponseBody
	public Map userHistorycount(@RequestBody User user, HttpServletRequest request) {
		try {
			MobileUserDomain mUserDomain = WebUtils.queryCurrentLoginInfo(request);
			String shopId = "abc";
			if (mUserDomain != null && mUserDomain.getMerchantInfoDetailModel() != null
					&& mUserDomain.getMerchantInfoDetailModel().getMerchantId() != null) {
				shopId = mUserDomain.getMerchantInfoDetailModel().getMerchantId();
			}
			user.setMerchantid(shopId);
			List<String> ids = new ArrayList<String>();

			Date begin = null;
			Date end = null;
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (user.getBeginDate() != null) {

				begin = formatter.parse(user.getBeginDate());
			}
			if (user.getEndDate() != null) {
				end = formatter.parse(user.getEndDate());
			}

			List<String> activeIds= new ArrayList<String>();
			List<String> inActiveIds= new ArrayList<String>();
			
			List<UserCount> allUser = userCountService.allUserlist(user);
			List<UserCount> activeUser = userCountService.historylist(user, begin, end);
			for(UserCount uc:allUser) {
				inActiveIds.add(uc.getUserId());
			}
			for(UserCount uc:activeUser) {
				activeIds.add(uc.getUserId());
				inActiveIds.remove(uc.getUserId());
			}
			
			allUser.removeAll(activeUser);
			Map rm = new HashMap();
			rm.put("activeuser", activeIds);
			rm.put("inactiveuser", inActiveIds);
			return CommonResponseUtils.successResponse(rm);
		} catch (ParseException e) {
			return CommonResponseUtils.failureResponse("日期转换错误");
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
	}

}
